﻿function showFeature(feature) {
	$("#feature-name").text(feature.name);
	$("#last-run").text(feature.lastRun);

	if (feature.userStory != null) {
		$("#user-story").html(feature.userStory);
		$("#user-story").show();
	} else {
		$("#user-story").hide();
	}

	if (feature.description != null) {
		$("#feature-description").text(feature.description);
		$("#feature-description").show();
	} else {
		$("#feature-description").hide();
	}

	var scenariosContainer = $("#scenario-list");
	scenariosContainer.empty();

	for (var scenarioIndex in feature.scenarios) {
		var scenario = feature.scenarios[scenarioIndex];
		scenariosContainer.append("<hr/>");

		var scenarioHeadline = $("<h3/>");
		scenarioHeadline.text(scenario.name);

		scenariosContainer.append(scenarioHeadline);

		var succeeded = true;

		for (var scenarioVersionIndex in scenario.scenarioVersions) {
			var scenarioVersion = scenario.scenarioVersions[scenarioVersionIndex];
			var stepContainer = $("<div class='steps'/>");

			for (var stepIndex in scenarioVersion.steps) {
				var step = scenarioVersion.steps[stepIndex];
				var stepElement = $("<p class='step'/>");

				stepElement.append("<span class='step-text'>" + step.text + "</span>");

				if (step.outcome == "Failed") {
					stepElement.addClass("failed");
					stepElement.append("<span class='failure-index'>" + step.failureIndex + "</span>");
					succeeded = false;
				}

				stepContainer.append(stepElement);
			}

			scenariosContainer.append(stepContainer);
		}

		if (succeeded) {
			scenarioHeadline.addClass("succeeded");
		}

		if (scenario.failureMessages.length > 0) {
			var ul = $("<ul class='failure-messages'/>");

			for (var failureIndex in scenario.failureMessages) {
				var failure = scenario.failureMessages[failureIndex];
				var li = $("<li><span class='ordinal'>" + (parseInt(failureIndex) + 1) + ")</span>" + failure + "</li>");
				ul.append(li);
			}

			scenariosContainer.append(ul);	
		}

		if (scenarioIndex < feature.scenarios.length - 1) {
			scenariosContainer.append("<hr/>");
		}
	}
};

nsList.init(json, $("#namespace-list"), showFeature);